<?php

namespace App\Http\Controllers;

use App\Models\Test;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Redis;

class IndexController extends Controller
{

    public function returnJson(int $code, array $data, string $msg = 'success'): JsonResponse
    {
        $data = [
            'code' => $code,
            'data' => $data,
            'msg'  => $msg,
        ];
        return response()->json($data);
    }

    public function text()
    {
        return $this->returnJson(1, [], 'text');
    }

    public function sleep(): JsonResponse
    {
        //处理程序要1秒
        sleep(1);
        return $this->returnJson(1, [], 'sleep');
    }

    public function redisSet(): JsonResponse
    {
        $key = 'test_key';
        Redis::set($key, time());
        return $this->returnJson(1, [], 'redisSet');
    }

    public function redisGet(): JsonResponse
    {
        $key = 'test_key';
        $ret = Redis::get($key);
        return $this->returnJson(1, [$ret], 'redisGet');
    }

    public function mysqlCreate(): JsonResponse
    {

        $data  = [
            'name'     => 'lzj_' . time(),
            'password' => md5('123456'),
            'set'      => 1,
        ];
        Test::create($data);
        return $this->returnJson(1, [], 'mysqlCreate');
    }

    public function mysqlShow(): JsonResponse
    {
        $ret = Test::find(1);
        return $this->returnJson(1, [], 'mysqlShow');
    }

}
